package com.example.logisticsingle.utils;


import com.example.logisticsingle.String.StringUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Map;

public class JsonUtils {
    private JsonUtils() {}
    private static ObjectMapper objectMapper;
    static {
        objectMapper = new ObjectMapper();
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
    }
    /**
     * 把对象转换成Json字符串
     * @param <T>
     * @param obj	待转换的Json对象
     * @return
     * @throws JsonProcessingException
     */
    public static <T> String toJson(T obj) throws JsonProcessingException {
        return objectMapper.writeValueAsString(obj);
    }

    /**
     * 把Json字符串转换成对象
     * @param json	Json字符串
     * @param clazz	对象类型
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T fromJson(String json, Class<T> clazz) throws JsonParseException,
            JsonMappingException, IOException {
        if (StringUtils.isNull(json)) {
            return null;
        }
        return objectMapper.readValue(json, clazz);
    }
    /**
     * 将对象转换成相应的类型
     * @param <T>
     * @param obj
     * @param clazz
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T fromJson(Object obj, Class<T> clazz)
            throws JsonParseException, JsonMappingException, IOException {
        if(obj!=null) {
            return objectMapper.convertValue(obj, clazz);
        }
        return null;
    }

    /**
     * 把String字符串转集合对象
     * @param json	Json字符串
     * @param clazz	对象类型
     * @param collectionClazz	集合类型
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    public static <E, T extends Collection<E>> T fromJson(String json, Class<E> clazz, Class<T> collectionClazz)
            throws JsonParseException, JsonMappingException, IOException {
        if (StringUtils.isNull(json)) {
            return null;
        }
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClazz, clazz);
        return objectMapper.readValue(json, javaType);
    }
    /**
     * 将对象转换成Map
     * @param obj待转换的对象
     * @return
     */
    public static Map<String, Object> convertObjToMap(Object obj){
        @SuppressWarnings("unchecked")
        Map<String,Object> map = objectMapper.convertValue(obj,Map.class);
        return map;
    }

}
